import {defHttp} from '/@/utils/http/axios';
import {useMessage} from '/@/hooks/web/useMessage';
import {useGlobSetting} from '/@/hooks/setting';
import {getCacheByDynKey, setCacheByDynKey} from '/@/utils/cache/index';

const glob = useGlobSetting();

const { createErrorModal } = useMessage();
enum Api {
  // 获取第三方oauth2授权地址
  thirdAuthRender = '/auth-service/thirdAuth/authorize',
  // 三方登录
  thirdAuthLogin = '/auth-service/thirdAuth/login',
  // 首次进行绑定
  firstBind = '/auth-service/thirdAuth/bind',
  //获取二维码信息
  getLoginQrcode = '/auth-service/thirdAuth/getLoginQrcode',
  //监控二维码扫描状态
  getQrcodeToken = '/auth-service/thirdAuth/getQrcodeToken',
}


/**
 * 获取第三方授权地址
 */
export const thirdAuthRender = ({ grantType, backUrl }) => {
  setCacheByDynKey('grantType', grantType)
  return defHttp.get({ url: `${Api.thirdAuthRender}/${glob.oauth2ClientId}/${grantType}`, params: { backUrl  } });
};

export const thirdAuthLogin = (params) => {
  const grantType = getCacheByDynKey('grantType')
  return defHttp.get({ url: `${Api.thirdAuthLogin}/${glob.oauth2ClientId}/${grantType}`, params });
};

export const thirdUserFirstBind = (params) => {
  return defHttp.post({ url: `${Api.firstBind}`, params }, { isTransformResponse: false });
};


/**
 * 获取登录二维码信息
 */
export function getLoginQrcode({ grantType }) {
  let url = Api.getLoginQrcode;
  return defHttp.get({ url: url, params: { clientId: glob.oauth2ClientId, grantType} });
}

/**
 * 监控扫码状态
 */
export function getQrcodeToken(params) {
  let url = Api.getQrcodeToken;
  return defHttp.get({ url: url, params }, { isTransformResponse: false });
}

